package main

import (
	"context"
	"embed"
	"github.com/sirupsen/logrus"
	"github.com/wailsapp/wails/v2"
	"github.com/wailsapp/wails/v2/pkg/options"
	"github.com/wailsapp/wails/v2/pkg/options/assetserver"
	view2 "miao-wails/backed/app/view"
	"miao-wails/backed/daemon"
	"miao-wails/backed/global"
	"miao-wails/backed/lib/log"
	"path/filepath"
)

//go:embed all:frontend/dist
var assets embed.FS

func main() {
	// 初始化配置文件
	err := global.InitConfig()
	if err != nil {
		panic(err)
	}
	// 初始化配置
	log.InitLog(filepath.Join(global.Conf.DataDir, "logs", global.Conf.Log.Name), global.Conf.Log.Lv)
	logrus.Infof("当前配置：%+v", global.Conf)
	// 启动后台服务
	dae := daemon.NewDaemon(global.Conf)
	go func() {
		dae.Run(context.Background())
	}()
	// Create an instance of the app structure
	app := view2.NewApp()
	secretTextView := view2.NewSecretText()
	idpView := view2.NewIdpView()
	// Create application with options
	err = wails.Run(&options.App{
		Title:  "hcy-app",
		Width:  1080,
		Height: 800,
		AssetServer: &assetserver.Options{
			Assets: assets,
		},
		Debug: options.Debug{
			OpenInspectorOnStartup: true, // 启动时打开开发者工具
		},
		BackgroundColour: &options.RGBA{R: 255, G: 255, B: 255, A: 1},
		OnStartup:        app.Startup,
		OnShutdown:       app.Shutdown,
		Bind: []interface{}{
			app,
			secretTextView,
			idpView,
		},
	})

	if err != nil {
		println("Error:", err.Error())
	}
}
